﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sinacor.Infra.UI.Settings;
using System.ComponentModel;
using PSE.Framework.UI.Settings;

namespace Sinacor.Infra.UI.Controls.SmartClient
{
    partial class SinacorDataGrid
    {
        #region IConfigurationComponent Members

        #region LoadConfiguration
        void IConfigurationComponent<SinacorConfigurationObjectType>.LoadConfiguration(IConfigurationObject<SinacorConfigurationObjectType> config)
        {
            if (config == null)
                throw new ArgumentNullException("config");
            if (!(config is DataGridConfigurationObject))
                throw new ArgumentException("The type of configuration object must be DataGridConfigurationObject", "config");

            DataGridConfigurationObject dataGridConfiguration = (DataGridConfigurationObject)config;

            // Configurações das colunas
            foreach (ColumnConfigurationObject columnConfiguration in dataGridConfiguration.Columns)
            {
                // Capturando coluna
                Column column = this.Columns[columnConfiguration.ConfigurationName];
                if (column == null)
                    throw new InvalidOperationException(string.Format("Column name '{0}' not found", columnConfiguration.ConfigurationName));

                // Indica se a coluna está visivel
                if (column.AllowChangeVisibility && columnConfiguration.Visible != null)
                    column.Visible = (bool)columnConfiguration.Visible;
                // Posição visual da coluna
                column.VisiblePosition = (columnConfiguration.VisiblePosition > -1 ? columnConfiguration.VisiblePosition : column.InternalColumn.VisiblePosition);

                // Largura da coluna
                column.Width = (columnConfiguration.Width > -1 ? columnConfiguration.Width : column.Width);
                // Indica que a largura foi ajustada pelo usuário
                column.IsWidthAdjustedByUser = (columnConfiguration.Width > -1);
            }

            // Ordenação
            var sortedColumns =
                from c in dataGridConfiguration.Columns
                where c.SortDirection != null
                orderby c.SortIndex
                select new { c.ConfigurationName, c.SortDirection };

            foreach (var col in sortedColumns)
            {
                this.Sort(this.Columns[col.ConfigurationName], (ListSortDirection)col.SortDirection);
            }

            // Número de colunas fixas
            if (dataGridConfiguration.FixedColumnCount > -1)
                this.FixedColumnCount = dataGridConfiguration.FixedColumnCount;
        }
        #endregion

        #region GetConfiguration
        IConfigurationObject<SinacorConfigurationObjectType> IConfigurationComponent<SinacorConfigurationObjectType>.GetConfiguration()
        {
            DataGridConfigurationObject dataGridConfiguration = new DataGridConfigurationObject();

            dataGridConfiguration.ConfigurationName = this.Name;

            // Configurações das colunas
            foreach (Column column in this.Columns)
            {
                ColumnConfigurationObject columnConfiguration = new ColumnConfigurationObject();

                // Nome da coluna
                columnConfiguration.ConfigurationName = column.Name;
                
                // Indica se a coluna está visivel
                columnConfiguration.Visible = (column.AllowChangeVisibility ? column.Visible : (bool?)null);
                // Posição visual da coluna
                columnConfiguration.VisiblePosition = column.VisiblePosition;

                // Largura da coluna
                columnConfiguration.Width = (column.IsWidthAdjustedByUser ? column.Width : -1);
                
                // Ordenação
                columnConfiguration.SortDirection = column.SortDirection;
                columnConfiguration.SortIndex = column.SortIndex;

                dataGridConfiguration.Columns.Add(columnConfiguration);
            }

            // Número de colunas fixas
            dataGridConfiguration.FixedColumnCount = this.FixedColumnCount;

            return dataGridConfiguration;
        }
        #endregion

        #endregion
    }
}
